		/**
		 * IndexedDB 管理类
		 * 封装了数据库的初始化、增删改查等操作
		 */
		class DBManager {
			constructor(dbName = 'RoadDataDB', storeName = 'road_data', version = 1) {
				this.dbName = dbName;
				this.storeName = storeName;
				this.version = version;
				this.db = null;
			}

			/**
			 * 打开（或创建）数据库和对象仓库
			 * @returns {Promise<IDBDatabase>}
			 */
			async openDB() {
				return new Promise((resolve, reject) => {
					if (this.db) {
						return resolve(this.db);
					}

					const request = indexedDB.open(this.dbName, this.version);

					request.onerror = (event) => {
						console.error('IndexedDB error:', event.target.error);
						reject(event.target.error);
					};

					request.onsuccess = (event) => {
						this.db = event.target.result;
						resolve(this.db);
					};

					// 当数据库版本更新或首次创建时调用
					request.onupgradeneeded = (event) => {
						const db = event.target.result;
						if (!db.objectStoreNames.contains(this.storeName)) {
							// 创建对象仓库，并指定 road_part_id 为主键
							const store = db.createObjectStore(this.storeName, { keyPath: 'road_part_id' });

							// 为经常用于查询的字段创建索引，以提高查询性能
							store.createIndex('road_id_idx', 'road_id', { unique: false });
							store.createIndex('road_name_idx', 'road_name', { unique: false });
							store.createIndex('road_class_idx', 'road_class', { unique: false });
							// ...可以根据需要为其他字段创建索引
						}
					};
				});
			}

			/**
			 * 将 GeoJSON Feature 转换为存储对象
			 * @param {object} feature GeoJSON Feature
			 * @returns {object} 存储在 IndexedDB 的对象
			 */
			_featureToDbObject(feature) {
				if (!feature || !feature.properties || !feature.geometry) {
					return null;
				}
				const dbObject = { ...feature.properties };
				// 将 geometry 也作为顶级属性存储
				dbObject.path_point = feature.geometry.coordinates; // 对应旧代码中的 path_point
				dbObject.geometry = feature.geometry; // 也可以存储完整的 geometry 对象
				
				// JSON字符串化一些字段，保持与旧逻辑一致
				if(dbObject.check_point) dbObject.check_point = JSON.stringify(dbObject.check_point);
				if(dbObject.route) dbObject.route = JSON.stringify(dbObject.route);

				return dbObject;
			}

			/**
			 * 将存储对象转换回 GeoJSON Feature
			 * @param {object} dbObject 存储在 IndexedDB 的对象
			 * @returns {object} GeoJSON Feature
			 */
			_dbObjectToFeature(dbObject) {
				const properties = { ...dbObject };
				// 从属性中移除 geometry 数据，避免重复
				delete properties.geometry;
				delete properties.path_point;
				
				// 解析JSON字符串字段
				try {
					if(properties.check_point) properties.check_point = JSON.parse(properties.check_point);
					if(properties.route) properties.route = JSON.parse(properties.route);
				} catch(e) {
					console.warn("解析属性失败", e);
				}

				return {
					type: 'Feature',
					properties: properties,
					geometry: dbObject.geometry || { // 兼容旧数据
						type: 'LineString', // 或者其他类型
						coordinates: dbObject.path_point || []
					}
				};
			}
			
			/**
			 * 将一组存储对象转换为 GeoJSON FeatureCollection
			 * @param {Array<object>} rows 存储对象数组
			 * @returns {object} GeoJSON FeatureCollection
			 */
			_exportToGeoJSON(rows) {
				return {
					type: 'FeatureCollection',
					features: rows.map(row => this._dbObjectToFeature(row))
				};
			}


			/**
			 * 添加或更新数据 (Upsert)
			 * @param {object} geojson_data GeoJSON FeatureCollection 对象
			 * @returns {Promise<number>} 受影响的记录数
			 */
			async addOrUpdateData(geojson_data) {
				const db = await this.openDB();
				const transaction = db.transaction(this.storeName, 'readwrite');
				const store = transaction.objectStore(this.storeName);
				let affected_count = 0;

				const promises = geojson_data.features.map(feature => {
					const dataObject = this._featureToDbObject(feature);
					if (dataObject && dataObject.road_part_id) {
						return new Promise((resolve, reject) => {
							// put 方法会自动处理新增或更新
							const request = store.put(dataObject);
							request.onsuccess = () => {
								affected_count++;
								resolve();
							};
							request.onerror = (event) => reject(event.target.error);
						});
					}
					return Promise.resolve();
				});

				await Promise.all(promises);

				return new Promise((resolve, reject) => {
					transaction.oncomplete = () => resolve(affected_count);
					transaction.onerror = (event) => reject(event.target.error);
				});
			}

			/**
			 * 检查数据是否存在
			 * @param {string} road_part_id 
			 * @returns {Promise<boolean>}
			 */
			async checkData(road_part_id) {
				const db = await this.openDB();
				const transaction = db.transaction(this.storeName, 'readonly');
				const store = transaction.objectStore(this.storeName);
				
				return new Promise((resolve, reject) => {
					// 使用 get 方法通过主键获取数据
					const request = store.get(road_part_id);
					request.onsuccess = () => {
						resolve(request.result !== undefined);
					};
					request.onerror = (event) => reject(event.target.error);
				});
			}
			
			/**
			 * 更新多条数据
			 * @param {Array<object>} data_arr - 每个对象包含 road_part_id 和要更新的字段
			 * @returns {Promise<number>} 更新的记录数
			 */
			async updateData(data_arr) {
				const db = await this.openDB();
				const transaction = db.transaction(this.storeName, 'readwrite');
				const store = transaction.objectStore(this.storeName);
				let updated_count = 0;

				const promises = data_arr.map(item => {
					if (!item.road_part_id) return Promise.resolve();

					return new Promise((resolve, reject) => {
						const getRequest = store.get(item.road_part_id);
						getRequest.onsuccess = () => {
							const existingData = getRequest.result;
							if (existingData) {
								// 合并新旧数据
								const updatedData = { ...existingData, ...item };
								const putRequest = store.put(updatedData);
								putRequest.onsuccess = () => {
									updated_count++;
									resolve();
								};
								putRequest.onerror = (event) => reject(event.target.error);
							} else {
								// 如果记录不存在，则不执行任何操作
								resolve();
							}
						};
						getRequest.onerror = (event) => reject(event.target.error);
					});
				});

				await Promise.all(promises);
				
				return new Promise((resolve, reject) => {
					transaction.oncomplete = () => resolve(updated_count);
					transaction.onerror = (event) => reject(event.target.error);
				});
			}


			/**
			 * 删除数据
			 * @param {Array<string>} road_part_id_arr 
			 * @returns {Promise<number>} 删除的记录数
			 */
			async deleteData(road_part_id_arr) {
				const db = await this.openDB();
				const transaction = db.transaction(this.storeName, 'readwrite');
				const store = transaction.objectStore(this.storeName);
				let deleted_count = 0;

				const promises = road_part_id_arr.map(id => {
					return new Promise((resolve, reject) => {
						const request = store.delete(id);
						request.onsuccess = () => {
							deleted_count++;
							resolve();
						};
						request.onerror = (event) => reject(event.target.error);
					});
				});

				await Promise.all(promises);

				return new Promise((resolve, reject) => {
					transaction.oncomplete = () => resolve(deleted_count);
					transaction.onerror = (event) => reject(event.target.error);
				});
			}

			/**
			 * 获取所有数据
			 * @returns {Promise<object>} GeoJSON FeatureCollection
			 */
			async getAllData() {
				const db = await this.openDB();
				const transaction = db.transaction(this.storeName, 'readonly');
				const store = transaction.objectStore(this.storeName);

				return new Promise((resolve, reject) => {
					const request = store.getAll();
					request.onsuccess = () => {
						resolve(this._exportToGeoJSON(request.result));
					};
					request.onerror = (event) => reject(event.target.error);
				});
			}
			
			/**
			 * 根据复杂条件筛选数据
			 * @param {object} filter_obj 筛选条件对象
			 * @returns {Promise<object>} GeoJSON FeatureCollection
			 */
			async getFilteredData(filter_obj) {
				const db = await this.openDB();
				const transaction = db.transaction(this.storeName, 'readonly');
				const store = transaction.objectStore(this.storeName);
				const results = [];

				return new Promise((resolve, reject) => {
					const request = store.openCursor();

					request.onerror = (event) => reject(event.target.error);

					request.onsuccess = (event) => {
						const cursor = event.target.result;
						if (cursor) {
							const record = cursor.value;
							let isMatch = true; // 假设此记录匹配

							// 1. 处理 filte_input_obj
							if (filter_obj.filte_input_obj) {
								for (const key in filter_obj.filte_input_obj) {
									const value = filter_obj.filte_input_obj[key];
									if (value === "" || value === undefined) continue;

									if (key === 'road_id') {
										const roadIds = value.split('-');
										if (roadIds.length === 1 && record.road_id !== roadIds[0]) {
											isMatch = false;
										} else if (roadIds.length === 3 && record.road_part_id !== value) {
											isMatch = false;
										} else if (roadIds.length === 2 && !record.road_part_id.startsWith(value + '-')) {
											isMatch = false;
										}
									} else {
										if (value === null && record[key] !== null) {
											isMatch = false;
										} else if (value !== null && record[key] !== value) {
											isMatch = false;
										}
									}
									if (!isMatch) break; // 如果已不匹配，跳出内层循环
								}
							}

							if (!isMatch) { // 如果 input 筛选不通过，则跳过 checkbox 筛选
								cursor.continue();
								return;
							}

							// 2. 处理 filte_checkbox_obj
							if (filter_obj.filte_checkbox_obj) {
								for (const key in filter_obj.filte_checkbox_obj) {
									const filte_arr = filter_obj.filte_checkbox_obj[key];
									if (!filte_arr || filte_arr.length === 0) continue;

									const hasNull = filte_arr.includes(null);
									const valuesOnly = filte_arr.filter(v => v !== null);

									let checkboxMatch = false;
									if (valuesOnly.length > 0 && valuesOnly.includes(record[key])) {
										checkboxMatch = true;
									}
									if (hasNull && record[key] === null) {
										checkboxMatch = true;
									}
									
									if (!checkboxMatch) {
										isMatch = false;
										break;
									}
								}
							}

							if (isMatch) {
								results.push(record);
							}

							cursor.continue();
						} else {
							// 游标遍历完成
							resolve(this._exportToGeoJSON(results));
						}
					};
				});
			}
		}
		// 导入 DBManager 类
		// import DBManager from './dbManager.js'; // 如果使用模块化

		// 实例化数据库管理器
		const dbManager = new DBManager();
		/**
		 * 更新数据。接受普通obj对象组成的数组。
		 * @param {Array<object>} data_arr - 对象数组，每个对象必须包含 road_part_id
		 * @returns {Promise<number>} 变更的数据条数
		 */
		async function update_data(data_arr) {
			try {
				// 前处理：将 "" 或 "暂无" 转换成 null
				const processed_arr = data_arr.map(data => {
					const newData = { ...data };
					for (let key in newData) {
						if (newData[key] === "" || newData[key] === "暂无") {
							newData[key] = null;
						}
					}
					return newData;
				});
				return await dbManager.updateData(processed_arr);
			} catch (error) {
				console.error("更新数据失败:", error);
				throw error;
			}
		}

		/**
		 * 检查数据是否存在。
		 * @param {string} road_part_id - 要检查的 ID
		 * @returns {Promise<boolean>} 有则返回true，没有返回false
		 */
		async function check_data(road_part_id) {
			try {
				return await dbManager.checkData(road_part_id);
			} catch (error) {
				console.error("检查数据失败:", error);
				throw error;
			}
		}

		/**
		 * 新增或更新数据。根据 road_part_id 查找，有则更新，没有则新增。
		 * @param {object} obj - GeoJSON FeatureCollection 格式的对象
		 * @returns {Promise<number>} 更新或新增的数据数量
		 */
		async function add_data(obj) {
			try {
				// 前处理：将 "暂无" 或 "null" 字符串转换为 null
				obj.features.forEach(feature => {
					for (let key in feature.properties) {
						if (feature.properties[key] === '暂无' || feature.properties[key] === 'null') {
							feature.properties[key] = null;
						}
					}
				});
				return await dbManager.addOrUpdateData(obj);
			} catch (error) {
				console.error("添加或更新数据失败:", error);
				throw error;
			}
		}

		/**
		 * 删除数据。
		 * @param {Array<string>} road_part_id_arr - 由 road_part_id 组成的数组
		 * @returns {Promise<number>} 成功删除的数量
		 */
		async function delete_data(road_part_id_arr) {
			try {
				return await dbManager.deleteData(road_part_id_arr);
			} catch (error) {
				console.error("删除数据失败:", error);
				throw error;
			}
		}

		/**
		 * 获取所有数据。
		 * @returns {Promise<object>} GeoJSON FeatureCollection 格式的数据
		 */
		async function get_all_data() {
			try {
				return await dbManager.getAllData();
			} catch (error) {
				console.error("获取全部数据失败:", error);
				throw error;
			}
		}

		/**
		 * 根据筛选参数获取数据。
		 * @param {object} filter_obj - 筛选对象 { filte_input_obj: {}, filte_checkbox_obj: {} }
		 * @returns {Promise<object>} GeoJSON FeatureCollection 格式的数据
		 */
		async function get_filte_data_from_server(filter_obj) {
			try {
				// 前处理：将 Set 转换为 Array
				if(filter_obj.filte_checkbox_obj) {
					for (let key in filter_obj.filte_checkbox_obj) {
						if (filter_obj.filte_checkbox_obj[key] instanceof Set) {
							filter_obj.filte_checkbox_obj[key] = Array.from(filter_obj.filte_checkbox_obj[key]);
						}
					}
				}
				return await dbManager.getFilteredData(filter_obj);
			} catch (error) {
				console.error("筛选数据失败:", error);
				throw error;
			}
		}